﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ATW.MT.BUS;
using System.Data;
using System.Text;
using Newtonsoft.Json;
using ATW.MT.CommonClass;
using System.Collections;

namespace ATW.MT.SellManage
{
    public partial class SupplyRange : System.Web.UI.Page
    {
        bus_C_Material dm = new bus_C_Material();
        bus_C_Size cs = new bus_C_Size();
        bus_C_Pressure cp = new bus_C_Pressure();
        bus_C_Conn cc = new bus_C_Conn();
        bus_C_Design cd = new bus_C_Design();
        bus_C_FranLevel cf = new bus_C_FranLevel();
        bus_C_StructLevel SL = new bus_C_StructLevel();
        bus_C_SealLevel Se = new bus_C_SealLevel();
        bus_T_ProductionArgs TP = new bus_T_ProductionArgs();
        bus_T_Supply TS = new bus_T_Supply();
        bus_T_production pro = new bus_T_production();
        bus_C_Temp temp = new bus_C_Temp();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                if (Request.QueryString["handler"] != null || Request.Form["handler"] != null)
                {
                    string handler = Request.Form["handler"] == null ? Request.QueryString["handler"] : Request.Form["handler"];
                    if (handler == "getProductList")
                    {
                        WriteRes(getProductionList());
                    }
                    if (handler == "getProductionUsed")
                    {
                        WriteRes(getProductionUsed());
                    }
                    if (handler == "setProduction")
                    {
                        WriteRes(setProduction());
                    }
                    if (handler == "getMaterialList")
                    {
                        WriteRes(getMaterialList());
                    }
                    if (handler == "getMaterialUsed")
                    {
                        WriteRes(getMaterialUsed());
                    }
                    if (handler == "setMaterial")
                    {
                        WriteRes(setMaterial());
                    }
                    if (handler == "getSizeList")
                    {
                        WriteRes(getSizeList());
                    }
                    if (handler == "getSizeUsed")
                    {
                        WriteRes(getSizeUsed());
                    }
                    if (handler == "setSize")
                    {
                        WriteRes(setSize());
                    }
                    if (handler == "getPressureList")
                    {
                        WriteRes(getPressureList());
                    }
                    if (handler == "getPressureUsed")
                    {
                        WriteRes(getPressureUsed());
                    }
                    if (handler == "setPressure")
                    {
                        WriteRes(setPressure());
                    }
                    if (handler == "getSealList")
                    {
                        WriteRes(getSealList());
                    }
                    if (handler == "getSealUsed")
                    {
                        WriteRes(getSealUsed());
                    }
                    if (handler == "setSeal")
                    {
                        WriteRes(setSeal());
                    }
                    if (handler == "getTempList")
                    {
                        WriteRes(getTempList());
                    }
                    if (handler == "getTempUsed")
                    {
                        WriteRes(getTempUsed());
                    }
                    if (handler == "setTemp")
                    {
                        WriteRes(setTemp());
                    }
                    if (handler == "delete")
                    {
                        WriteRes(Delete());
                    }
                }

            }
        }
        private void BindData()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["uid"]))
            {
                hdfuid.Value = Request.QueryString["uid"];
                lblSupplyer.Text = Request.QueryString["n"];
            }
        }
        #region 产品列表
        private string getProductionList()
        {
            try
            {
                if (!string.IsNullOrEmpty(Request.QueryString["u"]))
                {
                    string uid = Request.QueryString["u"];
                    if (common.filterChar(uid))
                    {
                        DataTable dt = pro.ProductionQuery(uid);
                        return JsonConvert.SerializeObject(dt);
                    }
                }
            }
            catch (Exception ex)
            {

            }
            return "";
        }
        private string getProductionUsed()
        {
            string uid = Request.QueryString["uid"];
            if (!common.filterChar(uid))
            {
                return "illigal args";
            }
            DataTable dt = TS.getSupplyProduction(uid);
            return JsonConvert.SerializeObject(dt);
        }
        private string setProduction()
        {
            try
            {
                string pid = Request.Form["pid"];
                string uid = Request.Form["uid"];
                if (!common.filterChar(pid) || !common.filterChar(uid))
                {
                    return "";
                }
                if (!TS.isProductionExist(pid, uid))
                {
                    int result = TS.addProduction(uid, pid);
                    if (result > 0)
                    {
                        return "OK";
                    }
                    else
                    {
                        return "DB Error";
                    }
                }

            }
            catch (Exception ex)
            {

            }
            return "";
        }
        #endregion
        #region 材质
        private string getMaterialList()
        {//获取材质列表
            string res = "";
            try
            {
                if (!string.IsNullOrEmpty(Request.QueryString["pid"]))
                {
                    string pid = Request.QueryString["pid"];
                    if (!common.filterChar(pid))
                    {
                        return "";
                    }
                    DataTable dt = dm.MaterialQuery(pid);
                    return JsonConvert.SerializeObject(dt);

                }

            }
            catch (Exception ex)
            {

            }
            return res;
        }
        private string getMaterialUsed()
        {//获取已使用材质
            try
            {
                string uid = Request.QueryString["uid"];
                string pid = Request.QueryString["pid"];
                if (!common.filterChar(uid) || !common.filterChar(pid))
                {
                    return "illigal args";
                }
                DataTable dt = TS.getSupplyMaterial(uid, pid);
                return JsonConvert.SerializeObject(dt);
            }
            catch (Exception ex)
            {

            }
            return "";
        }
        private string setMaterial()
        {
            try
            {
                string uid = Request.Form["uid"];
                string list = Request.Form["list"];
                string pid = Request.Form["pid"];
                if (!common.filterChar(uid) || !common.filterChar(list) || !common.filterChar(pid))
                {
                    return "illigal args";
                }
                ArrayList al = new ArrayList();
                al.Add(TS.removeSupplyMaterial(uid, pid));
                string[] _list = list.Split(new char[] { '|' });
                for (int i = 0; i < _list.Length; i++)
                {
                    al.Add(TS.addSupplyMaterial(uid, _list[i], pid));
                }
                if (TP.Exal(al))
                {
                    return "OK";
                }
                return "DB Error";

            }
            catch (Exception ex)
            {

            }
            return "";
        }
        #endregion
        #region 口径
        private string getSizeList()
        {
            try
            {
                string pid = Request.QueryString["pid"];
                DataTable dt = cs.SizeQuery(pid);
                return JsonConvert.SerializeObject(dt);
            }
            catch (Exception ex)
            {

            }
            return "";
        }
        private string getSizeUsed()
        {
            try
            {
                string uid = Request.QueryString["uid"];
                string pid = Request.QueryString["pid"];
                if (!common.filterChar(uid))
                {
                    return "illigal args";
                }
                DataTable dt = TS.getSupplySize(uid, pid);
                return JsonConvert.SerializeObject(dt);
            }
            catch (Exception ex)
            {

            }
            return "";
        }
        private string setSize()
        {
            try
            {
                string uid = Request.Form["uid"];
                string list = Request.Form["list"];
                string pid = Request.Form["pid"];
                if (!common.filterChar(uid) || !common.filterChar(list) || !common.filterChar(pid))
                {
                    return "illigal args";
                }
                ArrayList al = new ArrayList();
                al.Add(TS.removeSupplySize(uid, pid));
                string[] _list = list.Split(new char[] { '|' });
                for (int i = 0; i < _list.Length; i++)
                {
                    al.Add(TS.addSupplySize(uid, _list[i], pid));
                }
                if (TP.Exal(al))
                {
                    return "OK";
                }
                return "DB Error";
            }
            catch (Exception ex)
            {

            }
            return "";
        }
        #endregion
        #region 压力等级
        public string getPressureList()
        {
            try
            {
                string pid = Request.QueryString["pid"];
                if (!common.filterChar(pid))
                {
                    return "";
                }
                DataTable dt = cp.PressQuery(pid);
                return JsonConvert.SerializeObject(dt);
            }
            catch (Exception ex)
            {
            }
            return "";
        }
        public string getPressureUsed()
        {
            try
            {
                string uid = Request.QueryString["uid"];
                string pid = Request.QueryString["pid"];
                if (!common.filterChar(uid) || !common.filterChar(pid))
                {
                    return "illigal args";
                }
                DataTable dt = TS.getSupplyPressure(uid, pid);
                return JsonConvert.SerializeObject(dt);
            }
            catch (Exception ex)
            {

            }
            return "";
        }
        public string setPressure()
        {
            try
            {
                string uid = Request.Form["uid"];
                string list = Request.Form["list"];
                string pid = Request.Form["pid"];
                if (!common.filterChar(uid) || !common.filterChar(list))
                {
                    return "illigal args";
                }
                ArrayList al = new ArrayList();
                al.Add(TS.removeSupplyPressure(uid, pid));
                string[] _list = list.Split(new char[] { '|' });
                for (int i = 0; i < _list.Length; i++)
                {
                    al.Add(TS.addSupplyPressure(uid, _list[i], pid));
                }
                if (TP.Exal(al))
                {
                    return "OK";
                }
                return "DB Error";
            }
            catch (Exception ex)
            {

            }
            return "";
        }
        #endregion
        #region 密封等级
        private string getSealList()
        {
            try
            {
                string pid = Request.QueryString["pid"];
                if (!common.filterChar(pid))
                {
                    return "";
                }
                DataTable dt = Se.SealQuery(pid);
                return JsonConvert.SerializeObject(dt);
            }
            catch (Exception ex)
            {
            }
            return "";
        }
        private string getSealUsed()
        {
            try
            {
                string uid = Request.QueryString["uid"];
                string pid = Request.QueryString["pid"];
                if (!common.filterChar(uid) || !common.filterChar(pid))
                {
                    return "illigal args";
                }
                DataTable dt = TS.getSupplySeal(uid, pid);
                return JsonConvert.SerializeObject(dt);
            }
            catch (Exception ex)
            {
                return "";
            }
        }
        private string setSeal()
        {
            try
            {
                string uid = Request.Form["uid"];
                string list = Request.Form["list"];
                string pid = Request.Form["pid"];
                if (!common.filterChar(uid) || !common.filterChar(list) || !common.filterChar(pid))
                {
                    return "illigal args";
                }
                ArrayList al = new ArrayList();
                al.Add(TS.removeSupplySeal(uid, pid));
                string[] _list = list.Split(new char[] { '|' });
                for (int i = 0; i < _list.Length; i++)
                {
                    al.Add(TS.addSupplySeal(uid, _list[i], pid));
                }
                if (TP.Exal(al))
                {
                    return "OK";
                }
                return "DB Error";
            }
            catch (Exception ex)
            {
                return "";
            }
        }
        #endregion
        #region 温度
        private string getTempList()
        {
            try
            {
                DataTable dt = temp.TempPagerQuery("", 1, int.MaxValue);
                return JsonConvert.SerializeObject(dt);
            }
            catch (Exception ex)
            {
                return "";
            }
        }
        private string getTempUsed()
        {
            try
            {
                string uid = Request.QueryString["uid"];
                string pid = Request.QueryString["pid"];
                if (!common.filterChar(uid) || !common.filterChar(pid))
                {
                    return "illigal args";
                }
                DataTable dt = TS.getSupplyTemp(uid, pid);
                return JsonConvert.SerializeObject(dt);
            }
            catch (Exception ex)
            {
                return "";
            }
        }
        private string setTemp()
        {
            try
            {
                string uid = Request.Form["uid"];
                string list = Request.Form["list"];
                string pid = Request.Form["pid"];
                if (!common.filterChar(uid) || !common.filterChar(list) || !common.filterChar(pid))
                {
                    return "illigal args";
                }
                ArrayList al = new ArrayList();
                al.Add(TS.removeTemp(uid, pid));
                string[] _list = list.Split(new char[] { '|' });
                for (int i = 0; i < _list.Length; i++)
                {
                    al.Add(TS.addSuppluTemp(uid, _list[i], pid));
                }
                if (TP.Exal(al))
                {
                    return "OK";
                }
                return "DB Error";
            }
            catch (Exception ex)
            {
                return "";
            }
        }
        #endregion

        private string Delete()
        {
            try
            {
                string uid = Request.QueryString["uid"];
                string pid = Request.QueryString["pid"];
                if (!common.filterChar(uid) || !common.filterChar(pid))
                {
                    return "bad request";
                }
                if (pro.DelProduct(uid, pid))
                {
                    return "OK";
                }
                else
                {
                    return "DB Error";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        private void WriteRes(string s)
        {
            Response.Clear();
            Response.Write(s);
            Response.End();
        }
        private void Tips(string s)
        {//提示
            StringBuilder strScript = new StringBuilder();
            strScript.Append(@"<script language=javascript>
                var divTips=new jSharp.popUpDiv('divMsg');
                document.getElementById('msgContent').innerHTML='" + s + @"';
                divTips.Show();
                var t=setTimeout(function(){
                    divTips.Close();
                    clearTimeout(t);
                },1000);
            </script>");
            this.ClientScript.RegisterStartupScript(this.GetType(), "tips", strScript.ToString());
        }
    }
}